package com.cango.qutz.web.auth.menu.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.cango.qutz.web.auth.menu.service.IMenuService;
import com.cango.qutz.web.auth.resource.service.IResourceService;
import com.cango.qutz.web.entity.MenuEntity;
import com.cango.framework.base.pagination.PageInfo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cango.qutz.web.entity.ResourceEntity;
import com.cango.framework.base.AjaxReturnVO;
import com.cango.framework.base.controller.BaseController;
import com.cango.framework.base.pagination.PaginationResult;
import com.google.gson.Gson;

/**
 * <p>ClassName: MenuController</p>
 * <p>Description: 系统菜单Controller</p>
 * <p>Author: weih</p>
 * <p>Date: 2015-03-20</p>
 */
@Controller
@RequestMapping("/auth/menu")
public class MenuController extends BaseController {

    /**
     * log
     */
    private final Log log = LogFactory.getLog(MenuController.class);
    
    /**
     * <p>Field menuService: 系统菜单Service注入</p>
     */
    @Autowired
    private IMenuService menuService;
    /**
     * 资源组件
     */
    @Autowired
    private IResourceService resourceService;
    /**
     * <p>Description: 跳转系统菜单新增页面</p>
     * @param model （model）
     * @param menu (菜单对象)
     * @return String
     */
    @RequestMapping("/toAddMenu")
    public String toAddMenu(Model model,MenuEntity menu){
//        ResourceEntity resource=new ResourceEntity();
//        List<ResourceEntity> resources=resourceService.queryList(resource);
        model.addAttribute("menu",menu);
        return "menu/menuAdd";
    }
    /**
     * <p>Description: 跳转系统菜单列表页面</p>
     * @param menu  系统菜单对象
     * @return String
     */
    @RequestMapping("/list")
    public String list(MenuEntity menu){
        return "menu/menuList";
    }
    
    /**
     * <p>Description: 分页查询系统菜单记录</p>
     * @param menu 系统菜单对象
     * @param response response
     * @param pageInfo 分页对象
     */
    @RequestMapping("/getListData")
    public void getListData(MenuEntity menu,HttpServletResponse response,PageInfo pageInfo){
        PaginationResult<MenuEntity> result=menuService.queryListByPage(menu, pageInfo);
        super.ajaxJson(response, result);
    }
    /**
     * <p>Description: 保存系统菜单记录</p>
     * @param menu 系统菜单对象
     * @param response (response)
     */
    @RequestMapping("/saveMenu")
    public void saveMenu(MenuEntity menu,HttpServletResponse response){
        menu.setCreateUser(getSessionUser().getUserId());
        menu.setCreateTime(new Date());
        menu.setIsLeaf(0);
        menuService.insert(menu);
        log.info("用户"+getSessionUser().getUserName()+"新增了系统菜单:"+(new Gson()).toJson(menu));
        super.ajaxJson(response, AjaxReturnVO.success());
    }
    
    /**
     * 
     * 功能描述: <br>
     * 保存按钮
     * @param resource (resource对象)
     * @param response （response）
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/insertButton")
    public void insertButton(ResourceEntity resource, HttpServletResponse response) {
        boolean val = this.validButtonCode(resource.getResourceCode());
        if (val) {

            resource.setStatus(1);
            resourceService.insert(resource);
            super.ajaxJson(response, AjaxReturnVO.success());
        } else {
            super.ajaxJson(response, AjaxReturnVO.failed("该按钮代码已存在，请重新添加"));
        }
    }
     /**
      * 
      * 功能描述: <br>
      * 〈功能详细描述〉
      * @param resource (resource对象)
      * @param response （response）
      * @see [相关类/方法](可选)
      * @since [产品/模块版本](可选)
      */
    @RequestMapping("/delButton")
    public void delButton(ResourceEntity resource,HttpServletResponse response){
        resourceService.delete(resource);
        super.ajaxJson(response, AjaxReturnVO.success());
    }
    /**
     * <p>Description: 跳转系统菜单编辑页面</p>
     * @param menu 系统菜单对象
     * @param model (model)
     * @return String
     */
    @RequestMapping("toEditMenu")
    public String toEditMenu(MenuEntity menu, Model model){
        ResourceEntity resource=new ResourceEntity();
        resource.setMenuCode(menu.getMenuCode());
        resource.setStatus(1);
        List<ResourceEntity> resources=resourceService.queryList(resource);
        MenuEntity findmenu=menuService.queryObjectById(menu.getMenuCode());
        model.addAttribute("menuCode",menu.getMenuCode());
        model.addAttribute("menu",findmenu);
        model.addAttribute("resources",resources);
        return "menu/menuEdit";
    }
    
   /**
     * <p>Description: 保存系统菜单修改记录</p>
     * @param menu 系统菜单对象
     * @param response (response)
     */
    @RequestMapping("/editMenu")
    public void editMenu(MenuEntity menu, HttpServletResponse response){
        log.info("用户"+getSessionUser().getUserName()+"修改了系统菜单:"+(new Gson()).toJson(menu));
        menu.setUpdateUser(getSessionUser().getUserId());
        menu.setUpdateTime(new Date());
        menu.setIsLeaf(0);
        menuService.update(menu);
        super.ajaxJson(response, AjaxReturnVO.success());
    }
    /**
     * <p>Description: 得到单个系统菜单记录</p>
     * @param menu 系统菜单对象
     * @param response (response)
     */
    @RequestMapping("/getMenuById")
    public void getMenuById(MenuEntity menu, HttpServletResponse response){
        super.ajaxJson(response, menuService.queryObjectById(menu.getMenuCode()));
    }
    /**
     * <p>Description: 删除系统菜单记录</p>
     * @param menu 系统菜单对象
     * @param response (response)
     */
    @RequestMapping("deleteMenu")
    public void deleteMenu(MenuEntity menu, HttpServletResponse response){
        log.info("用户"+getSessionUser().getUserName()+"删除了系统菜单:"+(new Gson()).toJson(menu));
        menu.setUpdateUser(getSessionUser().getUserId());
        menu.setUpdateTime(new Date());
        menuService.delete(menu);
        super.ajaxJson(response, AjaxReturnVO.success());
    }
    
    /**
     * <p>Description: 查看系统菜单明细</p>
     * @param menu 系统菜单对象
     * @param model (model)
     * @return String
     */
    @RequestMapping("viewMenuDetail")
    public String viewMenuDetail(MenuEntity menu, Model model){
        model.addAttribute("menuCode",menu.getMenuCode());
        return "menu/menuDetail";
    }
    /**
     * <p>Description: 跳转到菜单树页面</p>
     * @return String
     */
    @RequestMapping("/toMenuTreeList")
    public String toMenuTreeList(){
        return "/menu/menutreelist";
    }
    
    /**
     * <p>Description: 跳转到菜单列表页面</p>
     * @param menu 系统菜单对象
     * @param model (model)
     * @return String
     */
    @RequestMapping("/forwardMenuChildrenList")
    public String forwardMenuChildrenList(MenuEntity menu, Model model){
        List<MenuEntity> menus=null;
        boolean change=false;
        //一级菜单
//        if("-1".equals(menu.getParentCode())){
//            menu.setParentCode("0");
//            change=true;
//        }
        menus=menuService.queryListByParentCode(menu);
        model.addAttribute("menus",menus);
//        if(change){
//            menu.setParentCode("-1");
//        }
        model.addAttribute("menu",menu);
        
        return "/menu/menuchildrenlist";
    }
    /**
     * <p>Description: 查询系统菜单所有记录</p>
     * @param menu 系统菜单所有对象
     * @param response response
     */
    @RequestMapping("/getListMenu")
    public void getListMenu(MenuEntity menu,HttpServletResponse response){
//        List<MenuTree> trees=new ArrayList<MenuTree>();
        List<MenuEntity> menulists=menuService.queryList(menu);
//        MenuTree treeone=new MenuTree("0","-1","菜单管理","","changelist('-1')");
//        trees.add(treeone);
//        for(MenuEntity entity:menulists){
//            MenuTree tree;
//            if("0".equals(entity.getParentCode())){
//                tree=new MenuTree(entity.getMenuCode(),entity.getParentCode(),
//                        entity.getMenuName(),"","changelist('"+entity.getMenuCode()+"')");
//            }else{
//                tree=new MenuTree(entity.getMenuCode(),entity.getParentCode(),entity.getMenuName(),"","");    
//            }
//            trees.add(tree);
//        }
        super.ajaxJson(response, menulists);
    }
    /**
     * <p>Description: 根据父级menu找到子级menu</p>
     * @param menu  (menu)
     * @param model (model)
     * @return list
     */
    @RequestMapping("findMenuByParentId")
    @ResponseBody
    public List<MenuEntity> findMenuByParentId(MenuEntity menu, Model model){
        List<MenuEntity> menus=menuService.queryListByParentCode(menu);
        return menus;
    }
    
    /**
     * 功能描述: <br>
     * 检验菜单按钮就的唯一性
     *
     *@param menuCode 菜单代码
     * @return 结果
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/validMenuCode")
    @ResponseBody
    public boolean validMenuCode(String menuCode){
        return this.menuService.validMenuCode(menuCode);
    }
    
    /**
     * 功能描述: <br>
     * 验证按钮代码唯一性
     *
     * @param buttonCode 按钮代码
     * @return 结果
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/validButtonCode")
    @ResponseBody
    public boolean validButtonCode(String buttonCode){
        return this.menuService.validButtonCode(buttonCode);
    }
}
